<?php

class bgAuthorize{
    
    public $permission = null;
    public $user = null;
    
	public function __construct(){
        $db = &JFactory::getDbo();
        $this->user = &JFactory::getUser();
        $userGroup = implode('',$this->user->groups);
        
        $query = "SELECT PRRM_INSERT,PRRM_UPDATE,PRRM_DELETE,PRRM_EDITOWN
                    FROM perm
                    WHERE PRRM_GRUM_CODE = '$userGroup'
                        AND PRRM_FUNC_CODE = '".JRequest::getWord('option')."'";
        $db->setQuery($query);
        $this->permission = $db->loadObject();
    }
    
    /**
     * bgAuthorize::checkAdd()
     * check permission add
     * @return
     */
    public function checkAdd(){
    	return $this->permission->PRRM_INSERT;
    }
    
    /**
     * bgAuthorize::checkDelete()
     * check permission delete
     * @return
     */
    public function checkDelete(){
    	return $this->permission->PRRM_DELETE;
    }
    
    /**
     * bgAuthorize::checkEdit()
     * check permission edit
     * @return
     */
    public function checkEdit(){
    	return $this->permission->PRRM_UPDATE;
    }
    
    /**
     * bgAuthorize::checkEditOwn()
     * check permission edit own
     * @return
     */
    public function checkEditOwn(){
    	return $this->permission->PRRM_EDITOWN;
    }
    
    /**
     * bgAuthorize::getPermission()
     * get all permission
     * @return
     */
    public function getPermission(){
    	return $this->permission;
    }
    
    public function getUserGroup(){
    	return implode(',', $this->user->groups);
    }
    
    public function getUserID(){
        return $this->user->id;
    }
    
    public function getOCBA_CODE(){
        return $this->user->USEM_OCBA_CODE;
    }
    
    public function getPerm($userGroup,$option){
        $query = "SELECT PRRM_INSERT,PRRM_UPDATE,PRRM_DELETE,PRRM_EDITOWN
                    FROM perm
                    WHERE PRRM_GRUM_CODE = '$userGroup'
                        AND PRRM_FUNC_CODE = '$option'";
                        
        $db = &JFactory::getDbo();
        $db->setQuery($query);
        $result = $db->loadObject();    
        return $result;
    }

    public function getPerm_frmessage_Office($option){
        $query = "SELECT PRRM_GRUM_CODE,PRRM_INSERT,PRRM_UPDATE,PRRM_DELETE,PRRM_EDITOWN
                    FROM perm
                    WHERE PRRM_FUNC_CODE = '$option' AND PRRM_GRUM_CODE IN (8,9,10)";
                        
        $db = &JFactory::getDbo();
        $db->setQuery($query);
        $result = $db->loadObjectList();    
        return $result;
    }

    public function getPerm_frmessage_CenterOrClub($option){
        $query = "SELECT PRRM_GRUM_CODE,PRRM_INSERT,PRRM_UPDATE,PRRM_DELETE,PRRM_EDITOWN
                    FROM perm
                    WHERE PRRM_FUNC_CODE = '$option' AND PRRM_GRUM_CODE IN (11,12)";
                        
        $db = &JFactory::getDbo();
        $db->setQuery($query);
        $result = $db->loadObjectList();    
        return $result;
    }
    
}